#pragma config(Hubs,  S1, HTMotor,  HTServo,  none,     none)
#pragma config(Hubs,  S2, HTMotor,  HTMotor,  none,     none)
#pragma config(Hubs,  S3, HTServo,  none,     none,     none)
#pragma config(Sensor, S1,     ,               sensorI2CMuxController)
#pragma config(Sensor, S2,     ,               sensorI2CMuxController)
#pragma config(Sensor, S3,     ,               sensorI2CMuxController)
#pragma config(Sensor, S4,     IRMux,          sensorI2CCustomFastSkipStates)
#pragma config(Motor,  motorA,           ,             tmotorNXT, openLoop, reversed, encoder)
#pragma config(Motor,  motorB,           ,             tmotorNXT, openLoop, reversed, encoder)
#pragma config(Motor,  motorC,           ,             tmotorNXT, openLoop, reversed, encoder)
#pragma config(Motor,  mtr_S1_C1_1,     RightBack,     tmotorTetrix, openLoop, reversed, encoder)
#pragma config(Motor,  mtr_S1_C1_2,     LeftBack,      tmotorTetrix, openLoop, encoder)
#pragma config(Motor,  mtr_S2_C1_1,     RightFront,    tmotorTetrix, openLoop, reversed, encoder)
#pragma config(Motor,  mtr_S2_C1_2,     LeftFront,     tmotorTetrix, openLoop, encoder)
#pragma config(Motor,  mtr_S2_C2_1,     SideLift,      tmotorTetrix, openLoop, encoder)
#pragma config(Motor,  mtr_S2_C2_2,     MiddleLift,    tmotorTetrix, openLoop, reversed, encoder)
#pragma config(Servo,  srvo_S1_C2_1,    servo1,               tServoNone)
#pragma config(Servo,  srvo_S1_C2_2,    servo2,               tServoNone)
#pragma config(Servo,  srvo_S1_C2_3,    servo3,               tServoNone)
#pragma config(Servo,  srvo_S1_C2_4,    servo4,               tServoNone)
#pragma config(Servo,  srvo_S1_C2_5,    servo5,               tServoNone)
#pragma config(Servo,  srvo_S1_C2_6,    servo6,               tServoNone)
#pragma config(Servo,  srvo_S3_C1_3,    LeftGripper2,         tServoStandard)
#pragma config(Servo,  srvo_S3_C1_4,    RightGripper2,        tServoStandard)
#pragma config(Servo,  srvo_S3_C1_5,    RightGripper1,        tServoStandard)
#pragma config(Servo,  srvo_S3_C1_6,    LeftGripper1,        tServoStandard)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "JoystickDriver.c"  //Include file to "handle" the Bluetooth messages.

/////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                                    initializeRobot
//
// Prior to the start of tele-op mode, you may want t o perform some initialization on your robot
// and the variables within your program.
//
// In most cases, you may not have to add any code to this function and it will remain "empty".
//
////////////////////////////////////////////////////////////////////////////////////////////////////

#define rightjoy joystick.joy1_y2/127.0*100.0
#define leftjoy joystick.joy1_y1/127.0*100.0
#define leftjoy2 -joystick.joy2_y2/127.0*90.0
#define rightjoy2 -joystick.joy2_y1/127.0*100.0

typedef struct
{
	bool pressed;
	bool open;
} servobuttons;

servobuttons frontGripper;
servobuttons backGripper;

void frontGripperAdjust(int position)
{
	servo[LeftGripper1]=255-position;
	servo[RightGripper1]=position;
}
void backGripperAdjust(int position)
{
	servo[LeftGripper2]=255-position;
	servo[RightGripper2]=position;
}

void initializeRobot()
{
	servoChangeRate[LeftGripper1]=5;
	servoChangeRate[LeftGripper2]=5;
	servoChangeRate[RightGripper1]=5;
	servoChangeRate[RightGripper2]=5;
	frontGripper.open=true;
	backGripper.open=true;
	frontGripperAdjust(0);
	backGripperAdjust(0);
	return;
}


/////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                                         Main Task
//
// The following is the main code for the tele-op robot operation. Customize as appropriate for
// your specific robot.
//
// Game controller / joystick information is sent periodically (about every 50 milliseconds) from
// the FMS (Field Management System) to the robot. Most tele-op programs will follow the following
// logic:
//   1. Loop forever repeating the following actions:
//   2. Get the latest game controller / joystick settings that have been received from the PC.
//   3. Perform appropriate actions based on the joystick + buttons settings. This is usually a
//      simple action:
//      *  Joystick values are usually directly translated into power levels for a motor or
//         position of a servo.
//      *  Buttons are usually used to start/stop a motor or cause a servo to move to a specific
//         position.
//   4. Repeat the loop.
//
// Your program needs to continuously loop because you need to continuously respond to changes in
// the game controller settings.
//
// At the end of the tele-op period, the FMS will autonmatically abort (stop) execution of the program.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////


task main()
{

	initializeRobot();

	waitForStart();   // wait for start of tele-op phase

	while (true)
	{
		bool flipped=false;
		bool slow=false;

		getJoystickSettings(joystick);

		if(joy1Btn(7)&&!flipped)
		{
			flipped=true;
			slow=!slow;
		}
		if(!joy1Btn(7))
		{
			flipped=false;
		}
		if(!slow)
		{
			if (abs(leftjoy)>10)
			{
				motor[LeftFront]=leftjoy;
				motor[LeftBack]=leftjoy;
			}
			else
			{
				motor[LeftFront]=0;
				motor[LeftBack]=0;
			}
			if (abs(rightjoy)>10)
			{
				motor[RightFront]=rightjoy;
				motor[RightBack]=rightjoy;
			}
			else
			{
				motor[RightFront]=0;
				motor[RightBack]=0;
			}
		}
		//slow
		else
		{
			if (abs(leftjoy)>10)
			{
				motor[LeftFront]=leftjoy/3;
				motor[LeftBack]=leftjoy/3;
			}
			else
			{
				motor[LeftFront]=0;
				motor[LeftBack]=0;
			}
			if (abs(rightjoy)>10)
			{
				motor[RightFront]=rightjoy/3;
				motor[RightBack]=rightjoy/3;
			}
			else
			{
				motor[RightFront]=0;
				motor[RightBack]=0;
			}
		}

		if (abs(leftjoy2)>10)
		{
			motor[SideLift]=-leftjoy2;
			motor[MiddleLift]=-leftjoy2;
		}
		else
		{
			motor[SideLift]=0;
			motor[MiddleLift]=0;
		}
		if((joy1Btn(4))&&!frontGripper.pressed)
		{
			frontGripper.pressed=true;
			frontGripper.open=!frontGripper.open;
		}
		if(!(joy1Btn(4)))
		{
			frontGripper.pressed=false;
		}
		if((joy1Btn(2))&&!backGripper.pressed)
		{
			backGripper.pressed=true;
			backGripper.open=!backGripper.open;
		}
		if(!(joy1Btn(2)))
		{
			backGripper.pressed=false;
		}
		if(joy1Btn(1))
		{
			frontGripper.open=false;
			backGripper.open=false;
			backGripperAdjust(128);
			wait10Msec(40);
			frontGripperAdjust(128);
		}
		if(joy1Btn(3))
		{
			frontGripper.open=true;
			backGripper.open=true;
		}
		if (!frontGripper.open)
		{
			frontGripperAdjust(128);
		}
		if (frontGripper.open)
		{
			frontGripperAdjust(0);
		}
		if (!backGripper.open)
		{
			backGripperAdjust(128);
		}
		if (backGripper.open)
		{
			backGripperAdjust(0);
		}
	}
}
